// add_cloth_page.dart
// import 'dart:io';
import 'dart:io';

import 'package:image_picker/image_picker.dart';

import 'package:flutter/material.dart';
// import '../../models/wardrobe.dart';
import '../../models/cloth.dart';

class add_cloth_page extends StatefulWidget {
  @override
  _add_cloth_pageState createState() => _add_cloth_pageState();
}

class _add_cloth_pageState extends State<add_cloth_page> {
  final _formKey = GlobalKey<FormState>();
  String clothname = '';
  String clothbelong = '';
  XFile? image;

  final ImagePicker _imagePicker = ImagePicker();

  void _submitForm() {
    // 这里可以添加代码来处理书签信息的保存逻辑
    if (_formKey.currentState!.validate()) {
      _formKey.currentState!.save();
      // 构建一个Bookmark对象（请根据实际情况调整Bookmark类的构造函数）
      final cloth = Cloth(
        clothname: clothname,
        wardrobeid: '',
        clothbelong: clothbelong,
      );

      cloth.clothimage = File(image!.path); // 设置书签图片路径

      // 使用Navigator.pop方法传递Bookmark对象回去
      Navigator.of(context).pop(cloth);
    }
  }

  void openImagePicker(BuildContext context) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return SizedBox(
            height: 270,
            child: Column(
              children: [
                ListTile(
                  leading: const Icon(Icons.photo_library),
                  title: const Text('从相册选择'),
                  onTap: () {
                    // 从相册选择图片
                    _imagePicker
                        .pickImage(source: ImageSource.gallery)
                        .then((selectedImage) {
                      if (selectedImage != null) {
                        setState(() {
                          image = selectedImage;
                        });
                      }
                    });
                    Navigator.pop(context);
                  },
                ),
                ListTile(
                  leading: const Icon(Icons.camera),
                  title: const Text('拍照'),
                  onTap: () {
                    // 拍照
                    Navigator.pop(context);
                  },
                ),
              ],
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('添加衣物')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TextFormField(
                decoration: const InputDecoration(labelText: '衣物名称'),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '请输入衣物名称';
                  }
                  return null;
                },
                onSaved: (value) => clothname = value!,
              ),
              TextFormField(
                decoration: const InputDecoration(labelText: '衣物归属'),
                keyboardType: TextInputType.url,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '请输入衣物归属';
                  }
                  return null;
                },
                onSaved: (value) => clothbelong = value!,
              ),
              const SizedBox(height: 16),
              OutlinedButton(
                style: OutlinedButton.styleFrom(
                  side: BorderSide(color: Colors.red), // 设置边框颜色
                ),
                onPressed: () {
                  openImagePicker(context);
                },
                child: Text('添加图片'),
              ),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: () {
                  _submitForm();
                },
                child: const Text('添加'),
              ),

              // 添加图片预览
              const SizedBox(height: 16),
              if (image != null)
                Image.file(
                  File(image!.path),
                  width: 200,
                  height: 200,
                ),
            ],
          ),
        ),
      ),
    );
  }
}
